package online.lmds.sdmanage.main.device.service;

import online.lmds.sdmanage.common.Status;
import online.lmds.sdmanage.common.entity.Page;
import online.lmds.sdmanage.common.entity.Result;
import online.lmds.sdmanage.main.authentication.dao.UserDao;
import online.lmds.sdmanage.main.authentication.entity.User;
import online.lmds.sdmanage.main.device.dao.DeviceDao;
import online.lmds.sdmanage.main.device.dao.DeviceLogDao;
import online.lmds.sdmanage.main.device.entity.Device;
import online.lmds.sdmanage.main.device.entity.DeviceLog;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * Created by Brighten Ma on 2017/4/30.
 */

@Service("DeviceLogService")
public class DeviceLogServiceImpl implements DeviceLogService{

    @Resource(name = "DeviceLogDao")
    private DeviceLogDao deviceLogDao;

    @Resource(name = "UserDao")

    private UserDao userDao;
    @Override
    public Result save(DeviceLog device) {
        Result result = new Result();
        if (device == null || device.getUserId() == null){
            return result.setErrorCode().setStatus(Status.BASE_USER_ID_ERROR);
        }
        User user = new User();
        user.setId(device.getUserId());
        User getUser = userDao.getById(user);
        if (getUser == null || getUser.getId() == null) {
            return result.setErrorCode().setStatus(Status.BASE_USER_ID_ERROR);
        }

        //若Android端没有生产uid 添加随机UID
        if (device.getUid().isEmpty()){
            device.setUid(UUID.randomUUID().toString());
        }
        deviceLogDao.save(device);
        if (device.getId() > 0){
            return getById(device);
        } else {
            return result.setErrorCode().setStatus(Status.UNKNOW).setMessage("添加失败");
        }
    }

    @Override
    public Result getById(DeviceLog device) {
        Result result = new Result();
        if (device == null || device.getId() == null){
            return result.setErrorCode().setStatus(Status.BASE_ID_ERROR);
        }

        DeviceLog getDeviceLog = deviceLogDao.getById(device);
        if (getDeviceLog == null || getDeviceLog.getId() == null) {
            return result.setErrorCode().setMessage("未找到指定id设备记录").setStatus(Status.BASE_ID_ERROR);
        }
        return result.setSuccessCode().setStatus(Status.SUCCEED).setData(getDeviceLog);
    }

    @Override
    public Result getByUid(DeviceLog device) {
        Result result = new Result();
        if (device == null || device.getUid() == null){
            return result.setErrorCode().setStatus(Status.BASE_UID_ERROR);
        }

        DeviceLog getDeviceLog = deviceLogDao.getByUid(device);
        if (getDeviceLog == null || getDeviceLog.getUid() == null) {
            return result.setErrorCode().setMessage("未找到指定uid设备记录").setStatus(Status.BASE_UID_ERROR);
        }
        return result.setSuccessCode().setStatus(Status.SUCCEED).setData(getDeviceLog);
    }

    @Override
    public Result list(DeviceLog log, Page page) {
        Result result = new Result();

        if (log == null || log.getUserId() == null) {
            return result.setErrorCode().setMessage("请先登录").setStatus(Status.BASE_NEED_LOGIN);
        }

        if ( (log.getUid() == null && log.getId() == null)) {
            return result.setErrorCode().setMessage("无效的设备id或uid").setStatus(Status.BASE_ID_ERROR);
        }
        List<DeviceLog> getList = deviceLogDao.list(log, page);
        return result.setSuccessCode().setStatus(Status.SUCCEED).setData(getList);
    }

    @Override
    public Result listByUserId(DeviceLog device, Page page) {
        Result result = new Result();
        if (device == null || device.getUserId() == null) {
            return result.setErrorCode().setMessage("无效的用户id").setStatus(Status.BASE_ID_ERROR);
        }

        List<DeviceLog> getList = deviceLogDao.list(device, page);
        return result.setSuccessCode().setStatus(Status.SUCCEED).setData(getList);
    }

    @Override
    public Result updateDeviceLogById(DeviceLog device) {
        Result result = new Result();
        if (device == null || device.getId() == null){
            return result.setErrorCode().setStatus(Status.BASE_ID_ERROR);
        }

        int rows = deviceLogDao.updateById(device);
        if (rows != 1) {
            return result.setErrorCode().setStatus(Status.UNKNOW).setMessage("更新设备记录失败");
        } else {
            return result.setSuccessCode().setStatus(Status.SUCCEED);
        }
    }

    @Override
    public Result updateDeviceLogByUid(DeviceLog device) {
        Result result = new Result();
        if (device == null || device.getUid() == null){
            return result.setErrorCode().setStatus(Status.BASE_UID_ERROR);
        }

        int rows = deviceLogDao.updateByUid(device);
        if (rows != 1) {
            return result.setErrorCode().setStatus(Status.UNKNOW).setMessage("更新设备记录失败");
        } else {
            return result.setSuccessCode().setStatus(Status.SUCCEED);
        }
    }

    @Override
    public Result updateStatusById(DeviceLog device) {
        Result result = new Result();
        if (device == null || device.getId() == null){
            return result.setErrorCode().setStatus(Status.BASE_ID_ERROR);
        }

        int rows = deviceLogDao.updateStatusById(device);
        if (rows != 1) {
            return result.setErrorCode().setStatus(Status.UNKNOW).setMessage("更新设备记录状态失败");
        } else {
            return result.setSuccessCode().setStatus(Status.SUCCEED);
        }
    }

    @Override
    public Result updateStatusByUid(DeviceLog device) {
        Result result = new Result();
        if (device == null || device.getUid() == null){
            return result.setErrorCode().setStatus(Status.BASE_UID_ERROR);
        }

        int rows = deviceLogDao.updateStatusByUid(device);
        if (rows != 1) {
            return result.setErrorCode().setStatus(Status.UNKNOW).setMessage("更新设备记录状态失败");
        } else {
            return result.setSuccessCode().setStatus(Status.SUCCEED);
        }
    }



}
